package org.lgiavedoni.android.franq.util.phone;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.lgiavedoni.android.franq.service.phone.ContextService;

import android.content.Context;
import android.os.Vibrator;

public class PhoneUtil {
	/*
	 * //private static final String
	 * phoneRegExp="\\(?\\b([0-9]{3})\\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})\\b";
	 * //private static final StringphoneRegExp=
	 * "/^((\\+\\d{1,3}(-| )?\\(?\\d\\)?(-| )?\\d{1,5})|(\\(?\\d{2,6}\\)?))(-| )?(\\d{3,4})(-| )?(\\d{4})(( x| ext)\\d{1,5}){0,1}$/"
	 * ; private static final String phoneRegExp="^([0-9]*\\-?\\ ?\\/?[0-9]*)$";
	 * 
	 * 
	 * //emailRe =
	 * /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int
	 * |mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/ //phoneRe =
	 * /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-|
	 * )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
	 * 
	 * 
	 * public static final String EXAMPLE_TEST = "02/343536 a";
	 * 
	 * public static void main3(String[] args) {
	 * System.out.println(EXAMPLE_TEST.matches(phoneRegExp)); String[]
	 * splitString = (EXAMPLE_TEST.split(phoneRegExp));
	 * System.out.println(splitString.length); for (String string : splitString)
	 * { System.out.println(string); } }
	 */

	/**
	 * TODO: Improve the method (rely basic at the moment)
	 * 
	 * @param text
	 * @return
	 */
	public static String[] getPhoneNumbersOfString(String text) {
		String regex = "\\d+";

		Pattern myPattern = Pattern.compile(regex);
		Matcher myMatcher = myPattern.matcher(text);

		List<String> result = new ArrayList<String>();
		while (myMatcher.find()) {
			String num_str = myMatcher.group(); // returns whole match
			if (num_str.length() > 4)
				result.add(num_str);
		}
		return result.toArray(new String[result.size()]);
	}

	public static void vibrate(long time) {
		if (ContextService.inst.getApplicationContext() != null) {
			((Vibrator) ContextService.inst.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE)).vibrate(time);
		}
	}

	public static void vibrateDefault() {
		vibrate(300);
	}

}
